package com.iflyun.rabbitmqcustmer.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.iflyun.rabbitmqcustmer.dao.WebProducerDao;
import com.iflyun.rabbitmqcustmer.domain.WebLog;



@Repository("webProducerDao")
public class WebProducerDaoImpl extends BaseDaoImpl<WebLog> implements WebProducerDao{

	
	@Resource(name="dataSource")
	private DataSource dataSource;


	
	
	@Override
	public int setWebLog(WebLog webLog) {
		this.save(webLog);
		return 1;
		
		
	}

	@Override
	public void saveWebLog(List<WebLog> list) {
		// TODO Auto-generated method stub
		this.batchSave(list);
		
	}

	@Override
	public void setWebLogList(List<WebLog> list) {
		JdbcTemplate jt= new JdbcTemplate(dataSource);
		// TODO Auto-generated method stub
		final List<WebLog> webLogList = list;
		String sql="insert into t_log_detail_web_temp values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		jt.batchUpdate(sql, new BatchPreparedStatementSetter(){
			public void setValues(PreparedStatement ps,int i)throws SQLException{
				String id=webLogList.get(i).getId();
				String userId=webLogList.get(i).getUserId();
				String ip=webLogList.get(i).getIp();
				String proxy_Ip=webLogList.get(i).getProxyIp();
				String cookieId=webLogList.get(i).getCookieId();
				String urlAbsoluteUrl=webLogList.get(i).getUrlAbsoluteUri();
				String jumpUrl=webLogList.get(i).getJumpUrl();
				String title=webLogList.get(i).getTitle();
				String physicalPath =webLogList.get(i).getPhysicalPath();
				String requestMode=webLogList.get(i).getRequestMode();
				String requestParameter = webLogList.get(i).getRequestParameter();
				String operationType = webLogList.get(i).getOperationType();
				String urlHost=webLogList.get(i).getUrlHost();
				String urlPort=webLogList.get(i).getUrlPort();
				String browseTime=webLogList.get(i).getBrowseTime();
				String isMobileDevice=webLogList.get(i).getIsMobileDevice();
				String browser=webLogList.get(i).getBrowser();
				String browserVersion=webLogList.get(i).getBrowserVersion();
				Timestamp accessTime=webLogList.get(i).getAccessTime();
				String productVersion=webLogList.get(i).getProductVersion();
				String productCode=webLogList.get(i).getProductCode();

		        ps.setString(1, id);
		        ps.setString(2, userId);
		        ps.setString(3, ip);
		        ps.setString(4, proxy_Ip);
		        ps.setString(5, cookieId);
		        ps.setString(6, urlAbsoluteUrl);
		        ps.setString(7, jumpUrl);
		        ps.setString(8, title);
		        ps.setString(9, physicalPath);
		        ps.setString(10, requestMode);
		        ps.setString(11, requestParameter);
		        ps.setString(12, operationType);
		        ps.setString(13, urlHost);
		        ps.setString(14, urlPort);
		        ps.setString(15, browseTime);
		        ps.setString(16, isMobileDevice);
		        ps.setString(17, browser);
		        ps.setString(18, browserVersion);
		        ps.setTimestamp(19, accessTime);
		        ps.setString(20, productVersion);
		        ps.setString(21, productCode);
		        
		       }
		       public int getBatchSize()
		       {
		        return webLogList.size();
		       }
			});
		
	}
	

}
